package com.cvo.scrumtoolkit.client.pannels;

import java.util.List;

import com.cvo.scrumtoolkit.client.controller.ControllerImpl;
import com.cvo.scrumtoolkit.client.controller.HistoryManager;
import com.cvo.scrumtoolkit.client.controller.Navigator;
import com.cvo.scrumtoolkit.client.controller.View;
import com.cvo.scrumtoolkit.client.entities.BacklogItem;
import com.cvo.scrumtoolkit.client.entities.LoadedProject;
import com.cvo.scrumtoolkit.client.entities.Project;
import com.cvo.scrumtoolkit.client.model.ProjectStore;
import com.cvo.scrumtoolkit.client.pannels.widgets.BacklogWidget;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class BacklogPanel extends Composite {
	
	
	private static BacklogPanelUiBinder uiBinder = GWT.create(BacklogPanelUiBinder.class);
	private final PersistentServiceAsync persistentService = GWT.create(PersistentService.class);
	String id;
	int sprint;
	private List<BacklogItem> sprintlist;	
	Timer t;
	interface BacklogPanelUiBinder extends UiBinder<Widget, BacklogPanel> {
	}
	@UiField 
	VerticalPanel verticalPanel;
	@UiField
	TextBox txtBacklogItem;
	@UiField
	Button btnSave;
	@UiField
	TextBox txtGeschatteTijd;
	@UiField
	Button startSprint;
	
	@UiHandler("btnSave")
	void onClick(ClickEvent e) {
		try {
			id =Navigator.getNavigator().getProjectId();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		persistentService.persistBacklogitem(txtBacklogItem.getText(),Integer.parseInt(txtGeschatteTijd.getText()), id, new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				System.out.println("Backlogitem is goed aangemaakt voor project: "+id);
				txtBacklogItem.setText("");
				txtGeschatteTijd.setText("");
			}
			
			@Override
			public void onFailure(Throwable caught) {
				System.out.println("Backlogitem is NIET goed aangemaakt voor project: "+id);
				
			}
		});
	//laadlijsten();
		wachteenbeetje();
	}
	@UiHandler("startSprint")
	void onClick1(ClickEvent e) {
  	
		
persistentService.setSprintsInProject(id, new AsyncCallback<Integer>() {
			

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Integer result) {
				sprint =result;
				System.out.println("Backlogitemlijst van project in de widget na verhoging: "+id+"  "+sprint);
				try{
					String string = "sprintbacklog/"+id+"/"+sprint;
					HistoryManager.gotoUrl(string);

				}catch (Exception ex) {
				// TODO: handle exception
				}
			}
		});


	
	}	
		
		public BacklogPanel(){
		initWidget(uiBinder.createAndBindUi(this));
		
		try {
			id =Navigator.getNavigator().getProjectId();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		persistentService.getSprintsInProject(id, new AsyncCallback<Integer>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Integer result) {
				sprint= result;
				System.out.println("Backlogitemlijst van project: "+id);
				
			}
		});
		
		laadlijsten();
		
		
		
	}
	
public void laadlijsten(){
	
	verticalPanel.clear();
	persistentService.getItems(id, new AsyncCallback<List<BacklogItem>>() {

		
		public void onFailure(Throwable caught) {
			System.out.println("Backlogitemlijst is NIET goed aangemaakt voor project: "+id);
			
		}

		public void onSuccess(List<BacklogItem> result) {
			for(BacklogItem backlogItem:result){
				if(backlogItem.getSprintnummer()<1){
				
					
					BacklogWidget backlogWidget = new BacklogWidget(backlogItem,sprint+1);
					verticalPanel.add(backlogWidget);
					System.out.println("Backlogitemlijst van project: "+id+"+sprintnummerdat hij meekrijgt: "+sprint);
				
				}
			
				
			}
		}
	});
persistentService.getItems(id, new AsyncCallback<List<BacklogItem>>() {

		
		public void onFailure(Throwable caught) {
			System.out.println("Backlogitemlijst is NIET goed aangemaakt voor project: "+id);
			
		}

		public void onSuccess(List<BacklogItem> result) {
			for(BacklogItem backlogItem:result){
				
			 if(backlogItem.getSprintnummer()>0){
			
					BacklogWidget backlogWidget = new BacklogWidget(backlogItem);
					verticalPanel.add(backlogWidget);
					backlogWidget.setStyleName("blauwknops");
					System.out.println("Backlogitem reeds in sprint: "+backlogItem.getBacklogItemName()+" "+backlogItem.getSprintnummer());
				};
				
			
			}
		}
	});
}	
public void wachteenbeetje(){
	Timer t= new Timer(){

		@Override
		public void run() {
	laadlijsten();
			
		}
		
	};
	t.schedule(6000);
}

}
